package com.gwl.concurrent.simple;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class ResponsiveUI implements Runnable {

	private static volatile double d = 1; 
	
	private String name; 
	public ResponsiveUI(String name) { 
		this.name = name; 
	}
	
	@Override
	public void run() { 
		try{ 
			while(true) { 
				d = d + (Math.PI + Math.E) / d; 
			} 
		} catch(Exception e) { 
			System.out.println(name + " :" + e.getMessage()); 
		} finally { 
			System.out.println(name + " :" + d); 
		}
	} 
	public static void main(String[] args) throws IOException { 
		ExecutorService exec = Executors.newCachedThreadPool(new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {
				Thread t = new Thread(r); 
				t.setDaemon(true); 
				return t;
			}
		}); 
		exec.execute(new ResponsiveUI("ui1")); 
		exec.execute(new ResponsiveUI("ui2")); 
		exec.execute(new ResponsiveUI("ui3")); 
		exec.execute(new ResponsiveUI("ui4")); 
		exec.execute(new ResponsiveUI("ui5")); 
		System.in.read(); 
		System.out.println("finally :" + d); 
	}

}
